/*
 * httphandleifmodifiedsince.cpp
 *
 *  Created on: Apr 27, 2009
 *      Author: kuncai
 */
#ifndef HTTPHANDLEIFMODIFIEDSINCE_H_
#define HTTPHANDLEIFMODIFIEDSINCE_H_
#include <sys/stat.h>
#include <unistd.h>
#include "log.h"
#include "httphandle.h"
#include "date.h"
using namespace Http;
class HttpHandleIfModifiedSince: public HttpHandle{
	void ProcessRequest(HttpRequest* httpRequest, HttpResponse* httpResponse){
		//read url file
		string sold_time="";
		sold_time = httpRequest->GetHeader(Http::IfModifiedSince);
		if(sold_time == ""){
			if(error != NULL){
				error->ProcessRequest(httpRequest, httpResponse);
			}
			return;
		}
		string sFilename = "."+httpRequest->GetParam("url"); 
		struct stat filestat;
		if (stat(sFilename.c_str(),&filestat)!=0){
			if(error != NULL){
				error->ProcessRequest(httpRequest, httpResponse);
			}
			return;
		}
		//success
		time_t now_time = filestat.st_mtime;
		Date date_file(now_time);
		Date date_now(date_file.ToString().c_str());
		Date date_old(sold_time);
		Log::Debug("date_now is %s\n",date_now.ToString().c_str());
		Log::Debug("date_old is %s\n",sold_time.c_str());
		if(date_now.Cmp(&date_old) > 0)
		{
			//有更改
			Log::Debug("difftime is %d\n",date_now.Cmp(&date_old));
			Log::Debug("ifmodifiedsince have change\n");
			if(error != NULL){
				error->ProcessRequest(httpRequest, httpResponse);
			}
		}
		else
		{
			//无更改，无需发送消息休
			Log::Debug("ifmodifiedsince have not change\n");
			httpResponse->SetStatus("304");
			httpResponse->SetReason("Not Modified");
			if(successor != NULL){
				successor->ProcessRequest(httpRequest, httpResponse);
			}
		}
		/*
		if(sold_time != "")
		{
			Log::Debug("ifmodifiedsince header is %s\n",sold_time.c_str());
			string sFilename = "."+httpRequest->GetParam("url"); 
			struct stat filestat;
			if (stat(sFilename.c_str(),&filestat)==0)
			{
				Log::Debug("now_time is getting\n");
				time_t now_time = filestat.st_mtime;
				time_t old_time = mktime(getdate(sold_time.c_str()));
				Log::Debug("now_time is %d,old_time is %d\n",now_time,old_time);
				if(difftime(now_time,old_time)>0)
				{
					//有更改
					Log::Debug("ifmodifiedsince have change\n");
					if(error != NULL){
						error->ProcessRequest(httpRequest, httpResponse);
					}
				}
				else
				{
					//无更改，无需发送消息休
					Log::Debug("ifmodifiedsince have not change\n");
					time_t now;
					time(&now);
					httpResponse->SetStatus("304");
					httpResponse->SetReason("Not Modified");
					httpResponse->SetHeader("Date", asctime(gmtime(&now)));
					if(successor != NULL){
						successor->ProcessRequest(httpRequest, httpResponse);
					}
				}
			}
			else
			{
				if(error != NULL){
					error->ProcessRequest(httpRequest, httpResponse);
				}
			}
		}
		else
		{
			Log::Debug("ifmodifiedsince error\n");
			if(error != NULL){
				error->ProcessRequest(httpRequest, httpResponse);
			}
		}
		*/
		//end read url file

	}
};
#endif /* HTTPHANDLEIFMODIFIEDSINCE_H_ */
